package service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.springframework.web.multipart.MultipartFile;

import util.Utilities;

import dao.SaleDAO;
import dto.ListSalePageDTO;
import dto.MemberDTO;
import dto.PagingDTO;
import dto.ProductDTO;
import dto.SaleListDTO;

public class SaleManageService {
	private SaleDAO dao;
	private String picutreDir;
	public SaleManageService(SaleDAO dao, String pictureDir) {
		this.dao = dao;
		this.picutreDir = pictureDir;
	}
	
	public ArrayList<SaleListDTO> selectSaleBy(String memId){
		return dao.selectSaleByMemId(memId);
	}
	
	public SaleListDTO selectSaleByNo(int saleNo){
		return dao.selectSaleByNo(saleNo);
	}
	
	public void modifySale(SaleListDTO saleDTO) throws IllegalStateException, IOException{
		MultipartFile upfile = saleDTO.getUpfile();
		if(upfile!=null&&!upfile.isEmpty()){
			String fileName = upfile.getOriginalFilename();
			String extension = fileName.substring(fileName.lastIndexOf("."));
			long time = System.currentTimeMillis();
			String newFileName = time+extension;
			upfile.transferTo(new File(picutreDir, fileName));
			saleDTO.setProductImage(newFileName);
		}
		dao.updateSaleByNo(saleDTO);
	}
	
	public void registSale(SaleListDTO saleDTO) throws IllegalStateException, IOException{
		MultipartFile upfile = saleDTO.getUpfile();
		if(upfile!=null && !upfile.isEmpty()){
			String fileName = upfile.getOriginalFilename();
			String extension = fileName.substring(fileName.lastIndexOf("."));
			long time = System.currentTimeMillis();
			String newFileName = time+extension;
			upfile.transferTo(new File(picutreDir, fileName));
			saleDTO.setProductImage(newFileName);
		}
		saleDTO.setSaleNo(dao.selectSaleNo());
		dao.insertSaleSelf(saleDTO);
	}
	
	
	public void delete(int saleNo){
		
		SaleListDTO saleDTO = dao.selectSaleByNo(saleNo);
		MultipartFile upfile =saleDTO.getUpfile();
		if(upfile!=null&&!upfile.isEmpty()){
			File file = new File(picutreDir,saleDTO.getProductImage() );
			file.delete();
			saleDTO.setProductImage(null);
		}
		dao.deleteSaleByNo(saleNo);
	}
	
	public void deleteFile(SaleListDTO saleDTO){
		dao.updatePictureFileName(saleDTO.getSaleNo());
		File file = new File(picutreDir,saleDTO.getProductImage() );
		file.delete();
		saleDTO.setProductImage(null);
	}
	
	public ArrayList<SaleListDTO> selectSaleAllListByPage(int page){
		return dao.selectSaleListByPaging(page);
	}
	
	public ListSalePageDTO getSaleListByPage(int page){
		ListSalePageDTO listSalePageDTO = null;
		ArrayList<SaleListDTO> saleList = dao.selectSaleListByPaging(page);
		int total = dao.selectSaleTotalContent();
		PagingDTO pdto = new PagingDTO(total, page);
		listSalePageDTO = new ListSalePageDTO(saleList,pdto);
		return listSalePageDTO;
	}
	
	public void modifyMemberMileage(MemberDTO mdto){
		dao.updateMemberMileage(mdto);
	}
	
	public ArrayList<ProductDTO> selectProductListByName(String productName){
		return dao.selectProductByProductName(productName);
	}
	
	public ArrayList<ProductDTO> selectProductListByPrice(int start, int end){
		return dao.selectProductByPrice(start, end);
	}
	
	public ArrayList<ProductDTO> selectProductListByCompany(String companyId){
		return dao.selectProductByCompany(companyId);
	}
	
	public ArrayList<ProductDTO> selectProductListToSale(HashMap map){
		return dao.selectProductToSale(map);
	}

	public ProductDTO selectProductByProductId(int productId) {
		return dao.selectProductByProductId(productId);
				
	}
	
}
